home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / footmain.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  14.1 KB  |  512 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8.                                               
  9. #include <stdlib.h>
  10. #include <mmsystem.hpp>
  11. #include "footmain.h"
  12. #include "instr.h"
  13. //---------------------------------------------------------------------------
  14. #pragma resource "*.dfm"
  15. TForm1 *Form1;
  16. char numbuf[10];
  17. //---------------------------------------------------------------------------
  18. __fastcall TForm1::TForm1(TComponent* Owner)
  19.   : TForm(Owner)
  20. {
  21. }
  22. //---------------------------------------------------------------------------
  23. void __fastcall TForm1::MoveForward(TObject *Sender)
  24. {
  25.     if ((OnOffSwitch->Position == 2) || (LastSack != NULL))
  26.         return;
  27.  
  28.     if (!running)
  29.         TogglePlay(true);
  30.  
  31.     if (Runningback->Left < TACKLEROW3 - 2)
  32.     {
  33.         if (field[rbx+1][rby].player != NULL)
  34.         {
  35.             Sacked(field[rbx+1][rby].player);
  36.             return;
  37.         }
  38.         field[rbx][rby].player = NULL;
  39.         field[rbx][rby].rb = false;
  40.         Runningback->Left += 22;
  41.         rbx++;
  42.     }
  43.     else
  44.     {
  45.         if (field[0][rby].player != NULL)
  46.         {
  47.             Sacked(field[0][rby].player);
  48.             return;
  49.         }
  50.         field[rbx][rby].player = NULL;
  51.         field[rbx][rby].rb = false;
  52.         Runningback->Left = RBLEFT;
  53.         rbx = 0;
  54.     }
  55.  
  56.     YardsToGo--;
  57.     FieldPosition++;
  58.     if (FieldPosition == 100)
  59.     {
  60.         sndPlaySound("td.wav", SND_SYNC);
  61.         running = false;
  62.         ShowField(false);
  63.         Home+=7;
  64.         HomeDisplay->Caption = itoa(Home, numbuf, 10);
  65.         FieldPosition=80;  // Starting position for visitor
  66.         Down = 1;
  67.         YardsToGo = 10;
  68.         Computer->Visible = true;
  69.         return;
  70.     }
  71.     field[rbx][rby].player = Runningback;
  72.     field[rbx][rby].rb = true;
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TForm1::MoveBack(TObject *Sender)
  76. {
  77.     if ((OnOffSwitch->Position == 2) || (LastSack != NULL))
  78.         return;
  79.  
  80.     if (!running)
  81.         TogglePlay(true);
  82.     if(FieldPosition > 0)
  83.     {
  84.       if (Runningback->Left > RBLEFT + 2)
  85.       {
  86.           if (field[rbx-1][rby].player != NULL)
  87.           {
  88.               Sacked(field[rbx-1][rby].player);
  89.               return;
  90.           }
  91.           field[rbx][rby].player = NULL;
  92.           field[rbx][rby].rb = false;
  93.           Runningback->Left -= 22;
  94.           rbx--;
  95.       }
  96.       else
  97.       {
  98.           if (field[9][rby].player != NULL)
  99.           {
  100.               Sacked(field[9][rby].player);
  101.               return;
  102.           }
  103.           field[rbx][rby].player = NULL;
  104.           field[rbx][rby].rb = false;
  105.           Runningback->Left = TACKLEROW3;
  106.           rbx = 9;
  107.       }
  108.  
  109.       YardsToGo++;
  110.       FieldPosition--;
  111.  
  112.       field[rbx][rby].player = Runningback;
  113.       field[rbx][rby].rb = true;
  114.     }
  115. }
  116. //---------------------------------------------------------------------
  117. void __fastcall TForm1::MoveUp(TObject *Sender)
  118. {
  119.     if ((OnOffSwitch->Position == 2) || (LastSack != NULL))
  120.         return;
  121.  
  122.     if (!running)
  123.         TogglePlay(true);
  124.  
  125.     if (Runningback->Top == RBTOP)
  126.         return; // already as up as it gets
  127.  
  128.     if (field[rbx][rby-1].player != NULL)
  129.     {
  130.         Sacked(field[rbx][rby-1].player);
  131.         return;
  132.     }
  133.  
  134.     if (Runningback->Top == RBBOTTOM)
  135.         Runningback->Top = RBMIDDLE;
  136.     else if (Runningback->Top == RBMIDDLE)
  137.         Runningback->Top = RBTOP;
  138.  
  139.     field[rbx][rby].player = NULL;
  140.     field[rbx][rby].rb = false;
  141.     rby--;
  142.     field[rbx][rby].player = Runningback;
  143.     field[rbx][rby].rb = true;
  144. }
  145. //---------------------------------------------------------------------
  146. void __fastcall TForm1::MoveDown(TObject *Sender)
  147. {
  148.     if ((OnOffSwitch->Position == 2) || (LastSack != NULL))
  149.         return;
  150.  
  151.     if (!running)
  152.         TogglePlay(true);
  153.  
  154.     if (Runningback->Top == RBBOTTOM)
  155.         return; // already as down as it gets
  156.  
  157.     if (field[rbx][rby+1].player != NULL)
  158.     {
  159.         Sacked(field[rbx][rby+1].player);
  160.         return;
  161.     }
  162.  
  163.     if (Runningback->Top == RBTOP)
  164.         Runningback->Top = RBMIDDLE;
  165.     else if (Runningback->Top == RBMIDDLE)
  166.         Runningback->Top = RBBOTTOM;
  167.  
  168.     field[rbx][rby].player = NULL;
  169.     field[rbx][rby].rb = false;
  170.     rby++;
  171.     field[rbx][rby].player = Runningback;
  172.     field[rbx][rby].rb = true;
  173. }
  174. //---------------------------------------------------------------------
  175. void __fastcall TForm1::Switched(TObject *Sender)
  176. {
  177.     switch (OnOffSwitch->Position)
  178.     {
  179.             case 1:    {
  180.                     Timer->Interval = 250;
  181.                     HomeDisplay->Visible = true;
  182.                     VisitorDisplay->Visible = true;
  183.                     TimeDisplay->Visible = true;
  184.                     ResetGame(NULL);
  185.                     break;
  186.                 }
  187.                 case 2:    {
  188.                     ShowField(false);
  189.                     HomeDisplay->Visible = false;
  190.                     VisitorDisplay->Visible = false;
  191.                     TimeDisplay->Visible = false;
  192.                     break;
  193.                 }
  194.                 case 3:    {
  195.                     Timer->Interval = 100;
  196.                     HomeDisplay->Visible = true;
  197.                     VisitorDisplay->Visible = true;
  198.                     TimeDisplay->Visible = true;
  199.                     ResetGame(NULL);
  200.                     break;
  201.                 }
  202.         }
  203. }
  204. //---------------------------------------------------------------------
  205. void __fastcall TForm1::ShowField(bool visible)
  206. {
  207.     Runningback->Visible = visible;
  208.     Tackler1->Visible = visible;
  209.     Tackler2->Visible = visible;
  210.     Tackler3->Visible = visible;
  211.     Tackler4->Visible = visible;
  212.     Tackler5->Visible = visible;
  213.  
  214. }
  215. //---------------------------------------------------------------------
  216. void __fastcall TForm1::TimerTimer(TObject *Sender)
  217. {
  218.     int x, y, newx, newy, direction;
  219.  
  220.     newy = y = random(3);
  221.     newx = x = random(10);
  222.     direction = random(2); // 0 is for x, 1 is for y
  223.  
  224.     if (!running)
  225.     {
  226.         if (LastSack != NULL)
  227.             LastSack->Visible = !LastSack->Visible;
  228.         return;
  229.     }
  230.  
  231.     if (field[x][y].rb == true)
  232.         return; // can't move the runningback!
  233.  
  234.     if (field[x][y].player == NULL)
  235.         return; // no tacker at this spot
  236.  
  237.     if (field[x][y].player->Left <= RBLEFT)
  238.         return; // already at the endzone
  239.  
  240.     if (direction == 0)  // we're moving horizontal
  241.     {
  242.         if (x < rbx)
  243.             newx = x +1;
  244.         else if (x > rbx)
  245.             newx = x -1;
  246.         else return;
  247.         //  we're already horizontally lined up with rb
  248.     }
  249.     else if (direction == 1)  // we're moving vertical
  250.     {
  251.         if (y < rby)
  252.             newy = y +1;
  253.         else if (y > rby)
  254.             newy = y -1;
  255.         else return;
  256.         //  we're already vertically lined up with rb
  257.     }
  258.  
  259.     if (field[newx][newy].rb == true) // got him!
  260.     {
  261.         Sacked(field[x][y].player);
  262.         return;
  263.     }
  264.  
  265.     if (field[newx][newy].player == NULL) // not blocked
  266.     {
  267.         field[x][y].player->Left -= (22 * (x - newx));
  268.         field[x][y].player->Top -= (16 * (y - newy));
  269.         field[newx][newy].player = field[x][y].player;
  270.         field[x][y].player = NULL;
  271.         return;
  272.     }
  273. }
  274. //---------------------------------------------------------------------
  275. void __fastcall TForm1::Sacked(TLabel *player)
  276. {
  277.     sndPlaySound("whistle.wav", SND_SYNC);
  278.     running = false;
  279.     LastSack = player;
  280.     if (YardsToGo <=0)
  281.     {
  282.         Down = 1;
  283.         YardsToGo = 10;
  284.     }
  285.     else
  286.     {
  287.         Down++;
  288.         if (Down > 4)
  289.         {
  290.             sndPlaySound("whistle.wav", SND_SYNC);
  291.             Down = 1;  // First down for visitor
  292.             YardsToGo = 10;
  293.             Computer->Visible = true;
  294.         }
  295.     }
  296. }
  297. //---------------------------------------------------------------------
  298. void __fastcall TForm1::ResetGame(TObject *Sender)
  299. {
  300.     YardsToGo=10;
  301.     Down=1;
  302.     FieldPosition=20;
  303.     Home=0;
  304.     Visitor=0;
  305.     Quarter = 1;
  306.     TimeLeft=15;
  307.     TimeDisplay->Caption = FloatToStrF(TimeLeft, ffGeneral, 4, 4);
  308.     HomeDisplay->Caption = itoa(Home, numbuf, 10);
  309.     VisitorDisplay->Caption = itoa(Visitor, numbuf, 10);
  310.     randomize;
  311.     ResetField();
  312. }
  313. //---------------------------------------------------------------------
  314. void __fastcall TForm1::ResetField()
  315. {
  316.     TogglePlay(false);
  317.     LastSack = NULL;
  318.  
  319.     // empty the field
  320.     for (int y=0; y<3; y++)
  321.         for (int x=0; x<10; x++)
  322.         {
  323.             field[x][y].player = NULL;
  324.             field[x][y].rb = false;
  325.         }
  326.  
  327.     // initial locations of players
  328.     field[0][1].player=Runningback;
  329.     field[0][1].rb=true;
  330.     rbx = 0;
  331.     rby = 1;
  332.     field[3][0].player=Tackler1;
  333.     field[3][1].player=Tackler2;
  334.     field[3][2].player=Tackler3;
  335.     field[5][1].player=Tackler4;
  336.     field[9][1].player=Tackler5;
  337.  
  338.     Runningback->Left = RBLEFT;
  339.     Runningback->Top = RBMIDDLE;
  340.     Tackler1->Left = TACKLEROW1;
  341.     Tackler1->Top = TACKLETOP;
  342.     Tackler2->Left = TACKLEROW1;
  343.     Tackler2->Top = TACKLEMIDDLE;
  344.     Tackler3->Left = TACKLEROW1;
  345.     Tackler3->Top = TACKLEBOTTOM;
  346.     Tackler4->Left = TACKLEROW2;
  347.     Tackler4->Top = TACKLEMIDDLE;
  348.     Tackler5->Left = TACKLEROW3;
  349.     Tackler5->Top = TACKLEMIDDLE;
  350.  
  351.     ShowField(true);
  352. }
  353. //---------------------------------------------------------------------
  354. void __fastcall TForm1::Exit1Click(TObject *Sender)
  355. {
  356.     Application->Terminate();  
  357. }
  358. //---------------------------------------------------------------------
  359. void __fastcall TForm1::About1Click(TObject *Sender)
  360. {
  361.     About->ShowModal();
  362. }
  363. //---------------------------------------------------------------------
  364. void __fastcall TForm1::ShowStats(TObject *Sender, TMouseButton Button,
  365.       TShiftState Shift, int X, int Y)
  366. {
  367.     if ((OnOffSwitch->Position == 2) || (running))
  368.         return;
  369.  
  370.     if (Quarter == 5)
  371.         return;
  372.  
  373.     YTGDisplay->Caption = itoa(YardsToGo, numbuf, 10);
  374.  
  375.     if (FieldPosition <= 50)
  376.     {
  377.         itoa(FieldPosition, numbuf, 10);
  378.         strcat(numbuf, " <");
  379.     }
  380.     else
  381.     {
  382.         strcpy(numbuf, "> ");
  383.         itoa(100 - FieldPosition, numbuf + 2, 10);
  384.     }
  385.     FieldPosDisplay->Caption = numbuf;
  386.  
  387.     DownDisplay->Caption = itoa(Down, numbuf, 10);
  388.  
  389.     ShowField(false);
  390.     ShowDisplay(true);
  391. }
  392. //---------------------------------------------------------------------
  393. void __fastcall TForm1::HideStats(TObject *Sender, TMouseButton Button,
  394.       TShiftState Shift, int X, int Y)
  395. {
  396.     if ((OnOffSwitch->Position == 2) || (running))
  397.         return;
  398.  
  399.     ShowDisplay(false);
  400.     if (!Computer->Visible)
  401.         ResetField();
  402. }
  403. //---------------------------------------------------------------------
  404. void __fastcall TForm1::ShowDisplay(bool visible)
  405. {
  406.     DownLabel->Visible = visible;
  407.     FieldPosLabel->Visible = visible;
  408.     YTGLabel->Visible = visible;
  409.     DownDisplay->Visible = visible;
  410.     FieldPosDisplay->Visible = visible;
  411.     YTGDisplay->Visible = visible;
  412. }
  413. //---------------------------------------------------------------------
  414. void __fastcall TForm1::Kick(TObject *Sender)
  415. {
  416.     if ((running) || (Down != 4))
  417.         return;  // button only valid just before fourth down
  418.  
  419.     ShowField(false);
  420.     FieldPosition += random(100);
  421.     if (FieldPosition >= 100)
  422.     {
  423.         sndPlaySound("td.wav", SND_SYNC);
  424.         Home+=3;
  425.         HomeDisplay->Caption = itoa(Home, numbuf, 10);
  426.         FieldPosition=80;  // Starting position for visitor
  427.       }
  428.     else
  429.     {
  430.         sndPlaySound("whistle.wav", SND_SYNC);
  431.         sndPlaySound("whistle.wav", SND_SYNC);
  432.     }
  433.     Down = 1;  // first down for visitor
  434.     YardsToGo = 10;
  435.     Computer->Visible = true;
  436.     LastSack = Runningback; // hack to keep movement keys disabled
  437. }
  438. //---------------------------------------------------------------------
  439. void __fastcall TForm1::ClockTick(TObject *Sender)
  440. {
  441.     if (!running)
  442.         return;
  443.  
  444.     sndPlaySound("tick.wav", SND_ASYNC);
  445.     TimeLeft -= 0.1;
  446.     TimeDisplay->Caption = FloatToStrF(TimeLeft, ffGeneral, 4, 4);
  447.  
  448.     if (TimeLeft <= 0)
  449.     {
  450.         Quarter++;
  451.         TimeLeft = 15;
  452.     }
  453.     if (Quarter >= 5)
  454.     {
  455.         // game over
  456.         sndPlaySound("whistle.wav", SND_SYNC);
  457.         sndPlaySound("whistle.wav", SND_SYNC);
  458.         LastSack = Runningback; // hack to keep movement keys disabled
  459.         TogglePlay(false);
  460.         ShowField(false);
  461.         ShowDisplay(true);
  462.     }
  463. }
  464. //---------------------------------------------------------------------
  465. void __fastcall TForm1::TogglePlay(bool toggle)
  466. {
  467.         running = toggle;
  468.         Timer->Enabled = toggle;
  469.         Clock->Enabled = toggle;
  470. }
  471. //---------------------------------------------------------------------
  472. void __fastcall TForm1::ComputerClick(TObject *Sender)
  473. {
  474.  
  475.     ShowField(false);
  476.     FieldPosition -= random(100);
  477.     if (FieldPosition <= 0)
  478.     {
  479.         sndPlaySound("td.wav", SND_ASYNC);
  480.         Visitor+=7;
  481.         VisitorDisplay->Caption = itoa(Visitor, numbuf, 10);
  482.         FieldPosition=20;
  483.     }
  484.     else
  485.     {
  486.         sndPlaySound("whistle.wav", SND_SYNC);
  487.         sndPlaySound("whistle.wav", SND_SYNC);
  488.     }
  489.     Computer->Visible = false;
  490.     LastSack = Runningback; // hack to keep movement keys disabled
  491. }
  492. //---------------------------------------------------------------------
  493. void __fastcall TForm1::FormKeyPress(TObject *Sender, char &Key)
  494. {
  495.   switch(Key)
  496.   {
  497.     case 'i':
  498.       MoveUp(Sender);
  499.       break;
  500.     case 'k':
  501.       MoveDown(Sender);
  502.       break;
  503.     case 'l':
  504.       MoveForward(Sender);
  505.       break;
  506.     case 'j':
  507.       MoveBack(Sender);
  508.       break;
  509.   }
  510. }
  511. //---------------------------------------------------------------------------
  512.